//jquery has already been included
//for each li with class "docs-user-roster", do X
console.log("gdocs-peertracker: just making sure I am loaded");

var autoscrollEnabled = false;
// timer might be inefficient. It'll be great if there's a DIV height change listener available
var pageTimer = null; // check for new page change every second
var pageCount = 0;
var currentPage = null;
var viewLoc = 0;
var pageHeight = 0;
var PULSE_TIME = 500;

$(document).ready(function(){
   //add a checkbox to "docs-title-outer" div
   $(".docs-title-outer").append("<div class=\"goog-inline-block tracker-title\">Enable Peer Tracker: <input type=\"checkbox\" name=\"gdocs-peertracker-activate\" id=\"gdocs-peertracker-activate\"/></div>");

   //now set a hook for a checkbox with id="gdocs-peertracker-activate"
   $("#gdocs-peertracker-activate").click(activatePeertrackerIfNeeded);
});

//The function that launches the peerTracker if needed
function activatePeertrackerIfNeeded() {
	console.log("gdocs-peertracker: checkbox clicked");
	//see if the box is checked, if not, do nothing, if yes, activate the peer tracker
	if($("#gdocs-peertracker-activate").is(':checked')){
		console.log("gdocs-peertracker: checkbox checked, going to activate");
      autoscrollEnabled = true;
		activateGdocsPeertracker();
	} else {
		console.log("gdocs-peertracker: checkbox not checked, doing nothing, maybe deactivate?");
      autoscrollEnabled = false;
      clearInterval(pageTimer);
	}
	
}


//The function that actually activates the PeerTracker bit
function activateGdocsPeertracker(){
	var test = $(".docs-user-roster");
	console.log("gdocs-peertracker: The list of users is: " + test); //this should log a non-blank object
	//TODO if this is null, show alert, uncheck the box
	if(test.length){
		//non-zero length
		console.log("gdocs-peertracker: all well, instrumenting the user list now");
		test.css("border","3px solid red");
      pageTimer = window.setInterval(checkPageCount, PULSE_TIME);
	} else {
		alert("Please wait for peer list to load before activating GDocs-PeerTracker");
		$("#gdocs-peertracker-activate").attr('checked',false);
	}
}


function checkPageCount(){
   if(autoscrollEnabled){
      var oldCount = pageCount;
      // get all the pages
      pages = $(".kix-page");
      // count how many pages there are
      pageCount = pages.length;
      if(pageCount != oldCount){
         // update data if there are changes in page count
         console.log("Number of pages changed! Count: " + pageCount);
         currentPage = pages.last().find(".kix-paragraphrenderer").first();
         pageHeight = pages.first().height();
      }
      // scroll to new position
      scrollToNewPosition();
   }
}


function scrollToNewPosition(){
   if(autoscrollEnabled){
      oldHeight = viewLoc;
      var upperSection = $("#docs-chrome").height() + $("#kix-ruler").height();
      var windowHeight = $(window).height();
      // FIXME: Get 4/5 of the document viewport because we don't want to scroll WAY down to the bottom,
      // so this way, we give user some space at the bottom for a more comfortable viewing
      // This is not a perfect view height, we may fix this if there's a need for a more 
      // precise height calculation.
      var docViewHeight = (windowHeight > upperSection) ? 4*(windowHeight - upperSection - 96)/5 : 0; // 96 is an approx space for the top doc padding

      // calculate the scroll location
      var totalDocHeight = (pageHeight * (pageCount-1)) + currentPage.height();
      viewLoc = (totalDocHeight < docViewHeight) ? 0 : totalDocHeight - docViewHeight;

      if(oldHeight != viewLoc){
         // if there are changes in height, then scroll to the new position
         $(".kix-appview-editor").animate({ scrollTop: viewLoc}, 250);
      }
   }
}

